home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Languguage OS 2
/
Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO
/
language
/
embedded
/
simulato
/
v2_3_mc6.tz
/
v2_3_mc6
/
testfiles
/
treefnd3.asm
< prev
next >
Wrap
Assembly Source File
|
1994-05-02
|
15KB
|
292 lines
; Program 3
; Binary Search Tree
; Due November 20, 1992
; This program will create and search a binary tree. The program will accept
; 4 commands:
; I [number] - to insert a number
; S [number] - to search for a number
; P - to print out the contents of the tree
; E - to exit the program
; only integers between 0 and 9 will be accepted
; each item in tree will have a root, one byte to hold number (0-9),
; then one blank byte, then
; a left pointer 4 bytes, and a right pointer 4 bytes.
; size of node is therefore 10 bytes
; data is displacement of 0
; left branch is displacement of 2
; right branch is displacement of 6
; NOTE: regarding input
; the program will ignore any number of spaces
; a number is required after the I and S commands (error message will be displayed)
; anything after the P or E command will be ignored. (no error message)
; only 1 digit numbers will be stored. The program will notify the user
; of what was inserted or searched for.
; Only the first digit will be used (ie The number 95 will be a 9)
; Loading origin is at $1000
org $1000 ;loading origin
;print title
jsr ClearScreen ;clear the screen
movea #Title,a0 ;move address of title into a0
jsr WriteString ;print title
; initialize registers
move.l #0,a2 ;zero out a2
move.l #0,a3 ;zero out a3
move.l #0,a4 ;zero out a4
; read from keyboard
BEGIN jsr WriteEOL ;print blank line
movea #INPUT,a0 ;get ready to print prompt
jsr WriteString ;prompt
movea #In_String,a0 ;move the address into a0
jsr ReadString ;read a string from the keyboard
jsr WriteEOL ;carriage return
movea #In_String,a0 ;designate the locate to store string
;break apart string, command goes into d2, number (if applicable) into d3
move.l #0,d2 ;zero out d2
move.l #0,d3 ;zero out d3
;get command put in d2
move.l #0,d5 ;zero out d5
LOOP1 move.b 0(a0,d5),d4 ;move next character into d4
cmpi.b #$20,d4 ;if space
beq.b INC1 ;then go to next character
move.b d4,d2 ;else store this as command
bra OUT1 ;jump out of loop
INC1 addq.l #1,d5 ;move to next character
bra LOOP1 ;loop thru again
OUT1 addq.l #1,d5 ;move to next char after command
;get the number put in d3
LOOP2 move.b 0(a0,d5),d4 ;move next char into d4
cmpi.b #$20,d4 ;if space
beq.b INC2 ;then go to next char
move.b d4,d3 ;else store this as the number
bra OUT2 ;jump out of loop
INC2 addq.l #1,d5 ;increment character counter
bra LOOP2 ;loop thru again
OUT2 cmpi.b #$50,d2 ;if command is Print
beq CHECK ;go to Check
cmpi.b #$45,d2 ;if command is Exit
beq CHECK ;go to check
cmpi.b #0,d3 ;if number is $0
beq.b BAD ;then it is I or S with no number
;check what was read in
CHECK cmpi.b #$49,d2 ;if insert command
beq.w INSERT ;then Insert the number
cmpi.b #$53,d2 ;if search command
beq.w SEARCH ;then search for the number
cmpi.b #$50,d2 ;if print command
beq.w PRINT ;then print contents of tree
cmpi.b #$45,d2 ;if exit command
beq.w EXIT ;then exit
BAD jsr WriteEOL ;print blank line
movea #BadInput,a0 ;get ready to print bad input message
jsr WriteString ;print out error message
bra BEGIN ;no valid input try again
;insert section ******************************************
INSERT cmpa #0,a2 ;see if empty
beq.w START ;then insert this number into tree
movea a2,a3 ;move tree pointer to top of tree.
COMPARE cmp.w 0(a3),d3 ;compare number to insert with root
beq.w EXIST ;if exist then print so.
blt.b GO_LEFT ;if less than, go left
bgt.b GO_RIGHT ;if greater than, go right
GO_LEFT cmpi.l #0,2(a3) ;see if null
bne 1$ ;no then jmp to move pointer
move.l SPACE,d0 ;move space into d0
jsr malloc ;allocate memory
move.l a0,2(a3) ;set the left pointer to this new location.
move.l 2(a3),a3 ;move the tree pointer to left branch
move.w d3,0(a3) ;move number into data field
move.l #0,2(a3) ;zero out left branch
move.l #0,6(a3) ;zero out right branch
bra END_INSERT ;jump out of insert section
1$ move.l 2(a3),a3 ;move the tree pointer to left branch
bra COMPARE ;compare again
GO_RIGHT cmpi.l #0,6(a3) ;see if null
bne 1$ ;no then jmp to move pointer
move.l SPACE,d0 ;move spaceinto d0
jsr malloc ;allocate memory
move.l a0,6(a3) ;set the right pointer to this new location.
move.l 6(a3),a3 ;move the tree pointer to right branch
move.w d3,0(a3) ;move number into data field
move.l #0,2(a3) ;zero out left branch
move.l #0,6(a3) ;zero out right branch
bra END_INSERT ;jump out of insert section
1$ move.l 6(a3),a3 ;move the tree pointer to right branch
bra COMPARE ;compare again
EXIST movea #String1,a0 ;move word 'integer ' into a0
jsr WriteString ;print word 'integer '
move.b d3,INTEGER ;move d3 to INTEGER
movea #INTEGER,a0 ;move integer into a0 to print
jsr WriteString ;print integer
movea #String2,a0 ;move string2 into a0
jsr WriteString ;print rest of line
bra BEGIN ;jump out of insert
START move.l SPACE,d0 ;set d0 for space
jsr malloc ;allocate memory
movea a0,a2 ;make a2 point to the newly allocated memory
move.w d3,0(a2) ;move the number into the root.
move.l #0,2(a2) ;zero out left pointer
move.l #0,6(a2) ;zero out right pointer
bra END_INSERT ;finished with insert
END_INSERT movea #String1,a0 ;move word 'integer ' into a0
jsr WriteString ;print word 'integer '
move.b d3,INTEGER ;move d3 to INTEGER
movea #INTEGER,a0 ;move integer into a0 to print
jsr WriteString ;print integer
movea #String6,a0 ;move string6 into a0
jsr WriteString ;print rest of line
bra BEGIN ;go back to read another command
; end insert section *******************************************
; search section ************************************************
SEARCH cmpa #0,a2 ;test if tree is empty
beq 5$ ;if empty print not found
move a2,a3 ;move the tree pointer to top
1$ cmp.w 0(a3),d3 ;compare the data with search
beq 4$ ;if equal then print found
blt 2$ ;if less than go left
bgt 3$ ;if greater than go right
2$ cmpi.l #0,2(a3) ;see if left branch is null
beq 5$ ;if null then print not found
move.l 2(a3),a3 ;move to left branch
bra 1$ ;compare again
3$ cmpi.l #0,6(a3) ;see if right branch is null
beq 5$ ;if null then print not found
move.l 6(a3),a3 ;move to right branch
bra 1$ ;compare again
4$ movea #String1,a0 ;move word into a0
jsr WriteString ;print word
move.b d3,INTEGER ;move d3 to INTEGER
movea #INTEGER,a0 ;move integer into a0 to print
jsr WriteString ;print integer
movea #String3,a0 ;move string2 into a0
jsr WriteString ;print rest of line
bra END_SRCH ;jump out of insert
5$ movea #String1,a0 ;move word 'integer ' into a0
jsr WriteString ;print word 'integer '
move.b d3,INTEGER ;move d3 to INTEGER
movea #INTEGER,a0 ;move integer into a0 to print
jsr WriteString ;print integer
movea #String4,a0 ;move string2 into a0
jsr WriteString ;print rest of line
bra END_SRCH ;jump out of insert
END_SRCH bra BEGIN ;read in another command
;end of search section *************************************
;PRINT section ************************************************
;print uses pre-order traversal (root-left-right)
PRINT cmpa #0,a2 ;see if tree is empty
beq EMPTY ;if empty then print empty
movea a2,a3 ;move the tree pointer to top
movea #STACK,a4 ;initialize stack pointer
movea #Tree,a0 ;move the word 'tree' to print
jsr WriteString ;print out the word 'tree'
;print root
AGAIN move.b 1(a3),ROOT ;move number into root
movea #ROOT,a0 ;move address of root into a0
jsr WriteString ;print root
;push right branch (if not null)
cmpi.l #0,6(a3) ;if right is null
beq.b 1$ ;then don't push onto stack
move.l 6(a3),-(a4) ;else push right branch on to stack
;go right (if not null)
1$ cmpi.l #0,2(a3) ;see if left is null
beq.b POP ;if null then pop off stack
move.l 2(a3),a3 ;else move pointer to left branch
bra AGAIN ;go thru again
;pop right branch (until no more left)
POP cmpi.l #0,(a4) ;see if stack is empty
beq.b END_PRNT ;if empty done
move.l (a4)+,a3 ;pop off stack the last right branch
bra AGAIN ;go thru cycle again
EMPTY movea #String5,a0 ;move empty into a0 to print
jsr WriteString ;print string5
bra END_PRNT ;end print section
END_PRNT jsr WriteEOL ;print blank line
bra BEGIN ;read in another command
; end print section *********************************************
;provided code for proper execution and exit
EXIT movea #GoodBye,a0 ;get ready to say good bye
jsr WriteString ;print goodbye
Move #228,D7 ;The code to end the program
Trap #14 ;Returns control to the operating system
NOLIST ;No list turns off listing so you only get your code.
DC.W 0 ;Forces a Word Boundary.
Include "samples.asm" ;Includes our routines in your program.
LIST
;define constants and variables
SPACE dc.l 10 ;size of node, 10 bytes
;define strings
Title dc.b '*********************************************',13,10
dc.b '* Binary Search Tree *',13,10
dc.b '* Program 3 Thomas Seddon *',13,10
dc.b '*********************************************',13,10,0
String1 dc.b 'Integer ',0
String2 dc.b ' already exists in the tree.',13,10,0
String3 dc.b ' was found in the search tree.',13,10,0
String4 dc.b ' was not found.',13,10,0
String5 dc.b 'The tree is empty.',13,10,0
String6 dc.b ' was inserted into the tree.',13,10,0
BadInput dc.b 'Bad Command! Try Again.',13,10,0
INPUT dc.b 'Command> ',0
Tree dc.b 'Tree Contents -> ',0
GoodBye dc.b 'GoodBye!',13,10,0
In_String ds.w 40 ;this is string for the readstring routine
I dc.b 'I'
S dc.b 'S'
P dc.b 'P'
E dc.b 'E'
ds.l 20 ;make enough space for stack
STACK dc.l 0 ;beginning of stack
INTEGER ds.b 1 ;temporary storage for integer
dc.b 0 ;null character
ROOT ds.b 1 ;temp storage for root to print
dc.b ' ' ;store comma
dc.b 0 ;null character
end ;end of program
; End of program ************************************************************